/**
 * Created by hhy on 2017/8/9.
 */
var vm = new Vue({
    el:'#ggapp',
    data:{
        hotList:[],
        articleList:[],
        hasPrev:true,
        hasNext:true,
        current:1,
        tagList:[]
    },
    methods: {
        getHotList: function(){
            $.get(baseURL + "blog/article/hot", function(r){
                var al = r.data;
                for(var index in al){
                    var item = al[index];
                    item.createTime = moment(item.createTime).format('YYYY/MM/DD');
                    vm.hotList.push(item);
                }
            });
        },
        getArticleList: function(pageNum){
            $.get(baseURL + "blog/article/list?pageNum="+pageNum, function(r){
                var al = r.data;
                for(var index in al){
                    var item = al[index];
                    item.createTime = moment(item.createTime).format('YYYY/MM/DD');
                    vm.articleList.push(item);
                }
                vm.hasPrev = r.current > 1;
                vm.hasNext = r.current < r.pages;
                vm.current = r.current;
                //var sendTime = moment(data.sendTime).format('HH:mm');
                //document.setTitle(r.data.title);
            });
        },
        getTagList: function(){
            $.get(baseURL + "blog/tag/list", function(r){
                vm.tagList = r.data;
            });
        }
    },
    beforeCreate: function(){

    },
    created: function(){
        //banner
        $.ajax({
            type : "get",
            url : baseURL + "blog/article/recommend",
            data : {},
            async : false,
            success : function(r){
                var al = r.data;
                for(var index in al){
                    var item = al[index];
                    item.createTime = moment(item.createTime).format('YYYY/MM/DD');
                    $(".am-slides").append(getOneBanner(item));
                }
            }
        });
        //articleList
        var articlePage = getQueryString("page");
        if(articlePage == null || articlePage+"".trim().length == 0){
            this.getArticleList(1);
        }else{
            this.getArticleList(articlePage);
        }
        this.getTagList();
        this.getHotList();
    },
    mounted:function(){

    }
});
function getOneBanner(item){
    var random = Math.floor(Math.random() * 3 + 1);
    var banner = ['<li>',
        '<img src="assets/i/b'+random+'.jpg">',
        '<div class="am-slider-desc blog-slider-desc">',
        '<div class="blog-text-center blog-slider-con">',
        '<span class="am-icon-user"></span>&nbsp;&nbsp;<span class="blog-color">'+item.author.nickname+' &nbsp;</span>',
        '<h1 class="blog-h-margin"><a href="article.html?id='+item.id+'">'+item.title+'</a></h1>',
        '<p>'+item.summary+'</p>',
        '<span>'+item.createTime+'</span>',
        '</div>',
        '</div>',
        '</li>'];
    return banner.join("");
}
$(function () {
    layui.use(['code'], function(){ //加载code模块
        layui.code({
            elem: 'pre', //默认值为.layui-code
            title: '程序代码',
            about: false
        });
    });
});